import { useRoute } from "vue-router"
import { RequestGoodsById } from "../apis/detail"
import { ref, type Ref, onMounted } from "vue"
import { Toast } from 'vant';
import type { ICartGoods, IGoods, ICollageGoods } from "../types/type"
import { useCartStore } from "@/stores/cart"//商品存入购物车
import { useCollectionStore } from '@/stores/collection'//商品收藏存储
import { usecollageGoodsListStore } from '@/stores/Detail-collage'//商品拼团存储
export const useDetail = () => {
    const route = useRoute()
    let id: any = route.query.id//接收home传过来的参数 //接受收藏页面的id
    const cartStore = useCartStore()
    const goods: Ref<IGoods> = ref({
        id: 0,
        shop: '',
        picture: '',
        product: '',
        price: 0,
        oldprice: 0,
        putaway: 0,
        detail: '',
        categoryname: '',
    })
    const bindBack = () => history.back() // 返回上一页即商品列表界面
    // 获取商品详情
    const getGoodsDetail = async () => {
        const data = await RequestGoodsById(id)
        const { resultCode, resultInfo } = data
        if (resultCode === 1) {
            goods.value = resultInfo

        }
    }
    // 首页加入购物车
    const getGoodsDetailByList = async (_id: number) => {
        const data = await RequestGoodsById(_id)
        const { resultCode, resultInfo } = data
        if (resultCode === 1) {
            goods.value = resultInfo
            id = _id
            bindAddCart()
        }
    }
    // 加入购物车
    const bindAddCart = () => {
        console.log("detail id》》》》", id);
        const cartGoods: ICartGoods = {
            id: goods.value.id,//添加商品时的id
            name: goods.value.product,
            price: goods.value.price,
            url: goods.value.picture,
            num: 1,//加入购物车，默认数量是1
            state: false,
            product: goods.value.product,
            oldprice: goods.value.oldprice,
            picture: goods.value.picture
        }
        cartStore.saveGoods(cartGoods)
        console.log("cartGoods", cartGoods)
        Toast.success('加入购物车成功!')
    }
    onMounted(() => {
        getGoodsDetail()
    })

    // 分享后弹出框
    let show = ref(false)
    const showPopup = () => {
        show.value = true;
    };
    const closePopup = () => { show.value = false }
    const prompt = () => {
        Toast("已分享")
    }

    // 添加收藏,按钮改变颜色，根据ID把数据刷选成数组，保存在pinia
    // 根据id调用商品详情接口
    // 保持到数组，[{id,name,url}]
    // localStoreage  pinia
    
    const colorone: Ref<Boolean> = ref(false)
    const AddcollectionList = () => {
        const collectionStore = useCollectionStore()
        colorone.value = !colorone.value
        if (colorone.value === true) {
            const collectionGoods: ICartGoods = {
                id: goods.value.id,//添加商品时的id
                name: goods.value.product,
                price: goods.value.price,
                url: goods.value.picture,
                num: 1,//加入购物车，默认数量是1
                state: false,
                product: goods.value.product,
                oldprice: goods.value.oldprice,
                picture: goods.value.picture,
                shop: ''
            }
            //传实参collectionGoods给saveCollectionGoods方法，然后使用pinia里面的方法。
            collectionStore.saveGoods(collectionGoods)
            Toast({
                message: '收藏成功',
                icon: 'like',
            });
        } else {
            Toast({
                message: '取消收藏',
                icon: 'like',
            });
        }
    }

    // 抢购倒计时
    const time = ref(5 * 60 * 60 * 1000);
    const AddcollageGoodsList = () => {
        const collageGoodsListStore = usecollageGoodsListStore()
        collageGoodsListStore.stateChange()
        console.log(collageGoodsListStore.stateAll);
        
        const detailCollageList: ICollageGoods = {
            id: goods.value.id,//添加商品时的id
            name: goods.value.product,
            price: goods.value.price,
            url: goods.value.picture,
            num: 1,//加入购物车，默认数量是1
            state: false,
            product: goods.value.product,
            oldprice: goods.value.oldprice,
            picture: goods.value.picture
        }
        //传实参collectionGoods给saveCollectionGoods方法，然后使用pinia里面的方法。
        collageGoodsListStore.saveCollageGoods(detailCollageList)
    }


    return { bindBack, goods, bindAddCart, getGoodsDetailByList, showPopup, show, closePopup, prompt, AddcollectionList, colorone, time, AddcollageGoodsList ,}
}
